package com.njworkorder.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.SectionAddDto;
import com.njworkorder.Entity.Section;
import com.njworkorder.Service.SectionService;
import com.njworkorder.Utils.NoCode;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.SectionListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "路段管理", description = "路段管理相关接口")
@RestController
@RequestMapping("/section")
public class SectionController {

    private final SectionService sectionService;
    public SectionController(SectionService sectionService) {
        this.sectionService = sectionService;
    }

    @Operation(summary = "添加路段信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody SectionAddDto sectionAddDto,
                                  @RequestHeader("Create-Ap-Id") String createApId){
        Section section = new Section();
        BeanUtils.copyProperties(sectionAddDto,section);
        section.setCreateApId(createApId);

        section.setSectionNo(NoCode.JSJK + NoCode.LD.name() + sectionService.getUpToDateSectionNo());

        boolean save = sectionService.save(section);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Operation(summary = "根据id更新路段信息")
    @PostMapping("/updateInfoById/{id}")
    public Result<String> updateInfoById(@RequestBody SectionAddDto sectionAddDto,
                                         @PathVariable("id") String id){

        Section section = new Section();
        BeanUtils.copyProperties(sectionAddDto,section);
        section.setId(id);

        boolean update = sectionService.updateById(section);
        if (update){
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.fail("更新失败");
    }

    @Operation(summary = "根据id获取路段信息")
    @GetMapping("/getInfoById")
    public Result<SectionListVo> getInfoById(@RequestParam("id") String id){
        MPJLambdaWrapper<Section> wrapper = sectionService.getMPJWrapper()
                .eq(Section::getId,id);
        SectionListVo sectionListVo = sectionService.selectJoinOne(SectionListVo.class, wrapper);

        String[] ks = getStake(sectionListVo.getStartStake());
        String[] eks = getStake(sectionListVo.getEndStake());
        sectionListVo.setStartPileNumUp(ks[0]);
        sectionListVo.setEndPileNumUp(ks[1]);
        sectionListVo.setStartPileNumDown(eks[0]);
        sectionListVo.setEndPileNumDown(eks[1]);

        return ResultUtil.success(sectionListVo);
    }

    @Operation(summary = "根据id删除路段信息")
    @GetMapping("/deleteInfoById")
    public Result<String> deleteInfoById(@RequestParam("id") String id){
        boolean remove = sectionService.removeById(id);
        if (remove){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @Operation(summary = "获取路段列表")
    @GetMapping("/getList")
    public Result<Page<SectionListVo>> getList(@RequestParam("pageIndex") int pageIndex,
                                               @RequestParam("pageSize") int pageSize,
                                               @RequestParam(value = "workspaceId", required = false) String workspaceId) {

        MPJLambdaWrapper<Section> wrapper = sectionService.getMPJWrapper()
                .apply("1=1")
                .eq(StringUtils.isNotEmpty(workspaceId), Section::getWorkspaceId, workspaceId);

        Page<SectionListVo> sectionListVoPage = sectionService.selectJoinListPage(new Page<>(pageIndex, pageSize), SectionListVo.class, wrapper);

        for (SectionListVo sectionListVo : sectionListVoPage.getRecords()) {
            String[] ks = getStake(sectionListVo.getStartStake());
            String[] eks = getStake(sectionListVo.getEndStake());

            if(ks.length == 0){ ks = new String[]{"",""};}
            if(eks.length == 0){ eks = new String[]{"",""};}

            sectionListVo.setStartPileNumUp(ks[0]);
            sectionListVo.setEndPileNumUp(ks[1]);
            sectionListVo.setStartPileNumDown(eks[0]);
            sectionListVo.setEndPileNumDown(eks[1]);
        }


        return ResultUtil.success(sectionListVoPage);
    }


    private String[] getStake(String stake){
        return stake.replace("K", "").split("[+]");
    }

}
